/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package org.cjava.system.bean.associate;

import java.io.Serializable;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import org.cjava.system.model.BusinessAssociate;
import org.cjava.system.model.State;
import org.cjava.system.service.interfac.IBusinessAssociateService;
import org.cjava.system.util.Clazz;
import org.cjava.system.util.faces.FacesUtil;

/**
 *
 * @author user
 */
@ManagedBean
@SessionScoped
public class HistoryAssociateBean implements Serializable {
    private static final long serialVersionUID = 1L;
    @ManagedProperty("#{businessAssociateService}")
    IBusinessAssociateService businessAssociateService;
    /////////////////////////////////////////////////////////////
    @ManagedProperty("#{businessAssociateBean}")
    BusinessAssociateBean businessAssociateBean;
    /////////////////////////////////////////////////////////////
    private BusinessAssociate businessAssociate = new BusinessAssociate();
    private String newCodeCard;
    
    public void initValuesLostCard(Integer idBusinessAssociate) throws Exception {
        
        businessAssociate = businessAssociateService.getByHQL("from BusinessAssociate b where b.id=" + idBusinessAssociate);
        
        newCodeCard = "";
        if(businessAssociate.getCodeCard() == null) {
            FacesUtil.addMessage("NO TIENE CARNET, DEBE ACTUALIZAR SUS DATOS", null, FacesMessage.SEVERITY_ERROR);
        } else if( businessAssociate.getCodeCard().equals("") ){
            FacesUtil.addMessage("NO TIENE CARNET, DEBE ACTUALIZAR SUS DATOS", null, FacesMessage.SEVERITY_ERROR);
        } else {
            FacesUtil.getRequestContext().execute("dialogChangeCard.show();");
        }
    }
    
    public void initValuesLostCard() {
        //System.out.println(businessAssociate);
        newCodeCard = "";
        if(businessAssociate.getCodeCard() == null) {
            FacesUtil.addMessage("NO TIENE CARNET, DEBE ACTUALIZAR SUS DATOS", null, FacesMessage.SEVERITY_ERROR);
        } else if( businessAssociate.getCodeCard().equals("") ){
            FacesUtil.addMessage("NO TIENE CARNET, DEBE ACTUALIZAR SUS DATOS", null, FacesMessage.SEVERITY_ERROR);
        } else {
            FacesUtil.getRequestContext().execute("dialogChangeCard.show();");
        }
    }
    
    public void saveAndChangeCodeCard() throws Exception {
        System.out.println("codigo nuevo: " + newCodeCard);
        String searchNewCode = businessAssociate.getIdApertureAssociate().getYear() + "" + newCodeCard;
        String hql = "select b.id from BusinessAssociate b where "
                + "b.codeCard='" + searchNewCode + "'";
        List<Integer> listIds = businessAssociateService.listHQL(hql);
        if( listIds.isEmpty() ) {
            //SE DEBE GUARDAR, DEBIDO A QUE NO HAY NINGUN CODIGO SIMILAR ^^
                //DEBEMOS ACTUALIZAR AL SOCIO A OTRO ESTADO
                State stateLostCard = businessAssociateBean.getClazz()
                        .STATE(Clazz._STATE._NAME._BUSINESS_ASSOCIATE_CARNET_PERDIDO, 
                                Clazz._STATE._ENTITY._BusinessAssociate);
                businessAssociate.setIdState(stateLostCard);
                businessAssociateService.update(businessAssociate);
            ///////////////////////////////////////////////////////////    
            businessAssociate.setId(null);
            stateLostCard = businessAssociateBean.getClazz()
                        .STATE(Clazz._STATE._NAME._BUSINESS_ASSOCIATE_CARNET_NUEVO, 
                                Clazz._STATE._ENTITY._BusinessAssociate);
            businessAssociate.setIdState(stateLostCard);
            businessAssociate.setCodeCard(searchNewCode);
            businessAssociateService.save(businessAssociate);
            ////////////////////////////////////////////////////////////////
            businessAssociateBean.initValuesViewAssociate();
            //businessAssociateBean.getListBusinessAssociate().add(businessAssociate);
            businessAssociate = new BusinessAssociate();
            //ACTUALIZANDO LA VISTA
            //FacesUtil.getRequestContext().update("formAssDownId:tableAssId");
            //CERRANDO EL DIALOGO 
            //FacesUtil.getRequestContext().execute("dialogChangeCard.hide();");
            FacesUtil.addMessage("Actualizado con éxito...", null, FacesMessage.SEVERITY_INFO);
        } else {
            //EL CODIGO YA EXISTE Y NO SE PUEDE DUPLICAR
            FacesUtil.addMessage("Código ya existe", null, FacesMessage.SEVERITY_ERROR);
        }
    }
            
    
    public void searchDetailHistory() {
        System.out.println("BuuAss: " + businessAssociate);
    }
    
    public BusinessAssociate getBusinessAssociate() {
        return businessAssociate;
    }

    public void setBusinessAssociate(BusinessAssociate businessAssociate) {
        this.businessAssociate = businessAssociate;
    }

    public String getNewCodeCard() {
        return newCodeCard;
    }

    public void setNewCodeCard(String newCodeCard) {
        this.newCodeCard = newCodeCard;
    }

    public void setBusinessAssociateService(IBusinessAssociateService businessAssociateService) {
        this.businessAssociateService = businessAssociateService;
    }

    public void setBusinessAssociateBean(BusinessAssociateBean businessAssociateBean) {
        this.businessAssociateBean = businessAssociateBean;
    }
    
    
    
}
